const Ajv = require("ajv") // version >= 8.0.0
const localize = require("ajv-i18n")
// or for JSON Type Definition
// const localize = require("ajv-i18n/localize/jtd")

const ajv =new  Ajv({allErrors: true, messages: false})


// 定义 JSON Schema
const schema = {
    type: 'object',
    properties: {
        name: { type: 'string' },
        age: { type: 'integer', minimum: 18 },
    },
    required: ['name', 'age'],
};

const validate = ajv.compile(schema)

// 数据示例
const data = {
    name: 'John',
    age: 15,
};
// 验证数据是否符合 JSON Schema
const valid = validate(data)

if (!valid) {
    // 指定语言为中文
    localize.zh(validate.errors);

    // 输出错误信息
    console.log(ajv.errorsText(validate.errors, { separator: '\n' }));
}